// pages/Contact/ContactChildren/ContactEdit/ContactEdit.js
import {get,post} from '../../../../utils/request'
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast'
Page({
  data: {
    show: false,
    areaList: null,
    val: '',
    areaDetail: '', //地址详情
    checked: false,
    username: '',
    phone: '',
    province: '',
    city: '',
    county: ''
  },
   //popup关闭
   onClose() {
    this.setData({
      show: false
    })
  },
  //popup展开
  openPopup() {
    this.setData({
      show: true
    })
  },
  isDefault({ detail }) {
    this.setData({ checked: detail });
  },
  selectArea(obj) {
    const address = obj.detail.values
    this.setData({
      province: address[0].name,
      city: address[1].name,
      county: address[2].name,
      show: false
    })
   
  },
  add() {
    const info = {
      name: this.data.username,
      tel: this.data.phone,
      province: this.data.province,
      city: this.data.city,
      county: this.data.county,
      addressDetail: this.data.areaDetail,
      isDefault: this.data.checked
    }
    // console.log(info);
    post({
      url: '/address/save',
      data: info
    }).then(res => {
      if(res.data.errno === 0) {
        Toast.success('添加成功')
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        },200)
      }
    })
  },
  onLoad: function (options) {

  },

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const db = wx.cloud.database()
    db.collection('area').get()
    .then(res => {
      // console.log(res.data[0]);
      delete res.data[0]._id
      this.setData({
        areaList: res.data[0]
      })
    })
    .catch(err => {
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})